// pages/adopt/messageJS/messageJS.js
var app = getApp();
import Api from '../../../api/api.js';
const api = new Api();
var timer;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    petDetailDic:{},
    pageIndex:1,
    pageCount:2000,
    userId2:"",
    userId:"",
    contentText:"",
    contentImage:"",
    myUserId:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.userId = options.toUserId;
    this.data.userId2 = options.fromUserId;
    this.getMessageList();
     this.addTimerData();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  addTimerData(){
    var _this = this;
   timer = setInterval(function () {
      _this.getMessageList();
    }, 1500);
  },
  getMessageList(){
  
    var _this = this;
    var map = {};
    map.pageCount = _this.data.pageCount;
    map.pageIndex = _this.data.pageIndex;
    map.userId2 = this.data.userId2;
    map.userId = this.data.userId;
    api.getRequestDataNoLoading(app.messagListUrl, map, true,"POST").then(res => {
      var list = res.list;
      console.log("list=====",JSON.stringify(list));
      _this.setData({
        dataList:list
      });

    });
  },
  contentInput(e){
    this.data.contentText = e.detail.value;
  },
  sendMSG(){
    this.sendMessage(1);
  },
  sendMessage(messageType){
    var _this = this;
    var map = {};
    map.TYPE = messageType;
    if (messageType == 1) {
      if (!this.data.contentText) {
        app.alert("请输入消息内容");
        return;
      }
      map.content = this.data.contentText;
     
    } else {
      map.content = this.data.contentImage;
    }
    map.fromUserId = this.data.userId;
    map.toUserId = this.data.userId2;
    map.type = messageType;
    console.log("map======",JSON.stringify(map));
    api.getRequestData(app.messageSaveUrl, map, false).then(res => {
      if (res.data.errorCode == 0) {
        // app.alert("发送成功~");
      } else {
        app.alert(res.data.errorMsg+"");
      }
    });
  },
  chosseImages() {
    var _this = this;
    app.chooseImageAndUpLoad(1).then(res => {
      if (res) {
        var url = res[0];
        _this.data.contentImage = url;
        _this.sendMessage(2);
      }
    });
  },
 
  preViewImages(e){
    var url =e.currentTarget.dataset.key;
    var _this = this;
    wx.previewImage({
      current: url,
      urls: [url],
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(timer);

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

    this.data.page = 1;
    this.getMessageList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // this.data.page = 1;
    // this.getMessageList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})